import { getUserInSSR, getUserFromLocalStorage } from '~/utils/auth'
// import { getUserFromLocalStorage } from '~/utils/auth'

export default function ({ isServer, store, req, route, redirect }) {
  // If nuxt generate, pass this middleware
  // console.log('调用中间件')
  if (isServer && !req) {
    return
  }
  console.log('是否为服务器认证' + isServer)
  const authUser = isServer ? getUserInSSR(req) : getUserFromLocalStorage()
  // const authUser = getUserFromLocalStorage()
  if (authUser && !store.state.authUser) {
    store.commit('SET_USER', authUser)
  } else if (authUser === null && route.name !== 'login') {
    redirect('/login')
  }
  // else if (!authUser && route.name !== 'login') {
  //   redirect('/login', {page: route.fullPath})
  // }
}
